package com.hzncc.flowable_diboot.service.processtransaction.impl;

import com.diboot.core.vo.Pagination;
import com.diboot.iam.service.IamUserService;
import com.diboot.iam.util.IamSecurityUtils;
import com.hzncc.flowable_diboot.dto.processtransaction.ProcessTaskDTO;
import com.hzncc.flowable_diboot.service.bpm.BpmProcessInstanceService;
import com.hzncc.flowable_diboot.service.bpm.BpmProcessTaskService;
import com.hzncc.flowable_diboot.util.DateUtils;
import com.hzncc.flowable_diboot.vo.bpm.BpmTaskReqVO;
import com.hzncc.flowable_diboot.vo.bpm.BpmTaskRespVO;
import com.hzncc.flowable_diboot.vo.processtransaction.ProcessTaskDetailVO;
import com.hzncc.flowable_diboot.vo.processtransaction.ProcessTaskListVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.springframework.stereotype.Service;

import com.diboot.core.util.S;
import com.diboot.core.service.impl.BaseServiceImpl;

import com.hzncc.flowable_diboot.entity.processtransaction.ProcessTask;

import com.hzncc.flowable_diboot.mapper.processtransaction.ProcessTaskMapper;
import com.hzncc.flowable_diboot.service.processtransaction.ProcessTaskService;

import javax.mail.UIDFolder;
import java.util.ArrayList;
import java.util.List;


/**
 * 我的待办 相关Service实现类
 *
 * @author MyName
 * @version 1.0
 * @date 2024-11-04
 * Copyright © MyCorp
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class ProcessTaskServiceImpl extends BaseServiceImpl<ProcessTaskMapper, ProcessTask> implements ProcessTaskService {

    private final TaskService taskService;

    private final RuntimeService runtimeService;

    private final IamUserService iamUserService;

    @Override
    public List<ProcessTaskListVO> getList(ProcessTaskDTO queryDto, Pagination pagination) {
        TaskQuery taskQuery = taskService.createTaskQuery()
                .taskAssignee(IamSecurityUtils.getCurrentUserId())
                .active()
                .orderByTaskCreateTime().desc();
        if (S.isNotEmpty(queryDto.getCurrentNode())) {
            taskQuery.taskNameLike("%" + queryDto.getCurrentNode() + "%");
        }
        if(S.isNotEmpty(queryDto.getType())){
            taskQuery.taskCategory(queryDto.getType());
        }
        if (S.isNotEmpty(queryDto.getBusinessTitle())) {
            List<ProcessInstance> list = runtimeService.createProcessInstanceQuery()
                    .processInstanceNameLike("%" + queryDto.getBusinessTitle() + "%")
                    .list();
            if (!list.isEmpty()) {
                taskQuery.processInstanceIdIn(list.stream().map(ProcessInstance::getId).toList());
            } else {
                return new ArrayList<>();
            }
        }
        List<Task> taskList = taskQuery.listPage((pagination.getPageSize() - 1) * pagination.getPageSize(), pagination.getPageSize());
        List<ProcessTaskListVO> list = new ArrayList<>();
        for (Task task : taskList) {
            list.add(convert(task));
        }
        return list;
    }

    private ProcessTaskDetailVO convert(Task task) {
        List<ProcessInstance> list = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).list();
        ProcessTaskDetailVO processTaskDetailVO = new ProcessTaskDetailVO();
        if (!list.isEmpty()) {
            ProcessInstance processInstance = list.get(0);
            processTaskDetailVO.setBusinessTitle(processInstance.getName());
            processTaskDetailVO.setProcessDefinitionId(processInstance.getProcessDefinitionId());
            processTaskDetailVO.setProcessInsId(processInstance.getId());
        }
        String userId = task.getAssignee();
        if (S.contains(userId, ":")) {
            userId = S.substringAfterLast(userId, ":");
        }
        processTaskDetailVO.setNodeAgent(task.getAssignee());
        processTaskDetailVO.setNodeAgentName(iamUserService.getEntity(userId).getRealname());
        processTaskDetailVO.setCurrentNode(task.getName());
        processTaskDetailVO.setId(task.getId());
        processTaskDetailVO.setStartTime(DateUtils.of(task.getCreateTime()));
        processTaskDetailVO.setType(task.getCategory());
        processTaskDetailVO.setTaskDefinitionKey(task.getTaskDefinitionKey());
        processTaskDetailVO.setPriority((long) task.getPriority());
        processTaskDetailVO.setSuspended(task.isSuspended());
        return processTaskDetailVO;
    }

    @Override
    public ProcessTaskDetailVO getDetailVO(String id) {
        List<Task> list = taskService.createTaskQuery().taskId(id).list();
        if (!list.isEmpty()) {
            return convert(list.get(0));
        }
        return null;
    }
}